package microcredit.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import microcredit.model.base.Model;
import microcredit.model.domain.LoanExtensionReason;
import microcredit.model.util.ModelUtils;

/**
 * Entity implementation class for Entity: MemberToLoanHistory
 *
 */
@Entity
public class MemberToLoanHistory extends Model implements Serializable {

	
	private static final long serialVersionUID = 1L;

	public MemberToLoanHistory() {
		super();
	}
   
	@Id
	@Column(name="MEMBER_ID", updatable=false, insertable=false)
	private Long memberID;
	
	@Id
	@Column(name="SEQUENCE_ID", updatable=false, insertable=false)
	private Long loanSequenceID;
	
	@Id
	@Column(name="HISTORYSEQUENCE_ID")
	private Long historySequenceID;
	
	@OneToOne()
	private MemberToLoan memberToLoan;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date startDate;
	
	@Column(columnDefinition=MONEY_DECIMAL)
	private BigDecimal repaymentRate;
	
	@Column(columnDefinition=MONEY_DECIMAL)
	private BigDecimal loanAmount;
	
	@Column(columnDefinition=MONEY_DECIMAL)
    private BigDecimal interestPercentage;
	
	private Integer newNumberOfWeeks;
	
	@Enumerated(EnumType.STRING)
	@Column(length=1)
	private LoanExtensionReason extensionReason;
	
	@Column(length=5000)
	private String reasonText;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false)
	private Date insertStamp;

	@PrePersist
	public void prePersist() throws ValidationException {
		if (getInsertStamp() == null) {
			setInsertStamp(ModelUtils.createTimestamp());
		}
		validate();
	}
	
	@PreUpdate
	public void preUpdate() throws ValidationException {
		validate();
	}
	
	public void validate() throws ValidationException {
		//TODO a lot more validations necessary
	}
	
	public void setInterestPercentage(BigDecimal interestPercentage) {
		this.interestPercentage = interestPercentage;
	}
	
	public BigDecimal getInterestPercentage() {
		return interestPercentage;
	}
	
	public BigDecimal getLoanAmount() {
		return loanAmount;
	}

	public void setLoanAmount(BigDecimal loanAmount) {
		this.loanAmount = loanAmount;
	}

	public BigDecimal getRepaymentRate() {
		return repaymentRate;
	}
	
	public void setRepaymentRate(BigDecimal repaymentRate) {
		this.repaymentRate = repaymentRate;
	}
	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Long getHistorySequenceID() {
		return historySequenceID;
	}

	public void setHistorySequenceID(long historySequenceID) {
		this.historySequenceID = historySequenceID;
	}

	public MemberToLoan getMemberToLoan() {
		return memberToLoan;
	}

	public void setMemberToLoan(MemberToLoan memberToLoan) {
		this.memberToLoan = memberToLoan;
	}

	public Long getMemberID() {
		return memberID;
	}

	public Long getLoanSequenceID() {
		return loanSequenceID;
	}
	
	public Integer getNewNumberOfWeeks() {
		return newNumberOfWeeks;
	}

	public void setNewNumberOfWeeks(Integer newNumberOfWeeks) {
		this.newNumberOfWeeks = newNumberOfWeeks;
	}

	public LoanExtensionReason getExtensionReason() {
		return extensionReason;
	}

	public void setExtensionReason(LoanExtensionReason extensionReason) {
		this.extensionReason = extensionReason;
	}

	public String getReasonText() {
		return reasonText;
	}

	public void setReasonText(String reasonText) {
		this.reasonText = reasonText;
	}

	public Date getInsertStamp() {
		return insertStamp;
	}

	public void setInsertStamp(Date insertStamp) {
		this.insertStamp = insertStamp;
	}
	
	
	@Override
	public String toString() {
		return "MemberToLoanHistory ["+getMemberID()+", "+getLoanSequenceID()+", "+getHistorySequenceID()+"]";
	}
	
}
